package main

import (
	"context"
	"fmt"
	"time"

	"github.com/louis-xie-programmer/go-local-cache/cache"
)

func main() {
	c := cache.NewCache(cache.CacheOptions{
		DefaultTTL:    10 * time.Second,
		EmptyTTL:      2 * time.Second,
		RefreshFactor: 0.3,
		ShardCount:    8,
		TotalMaxBytes: 1 << 20,
	})

	loader := func(ctx context.Context) (interface{}, time.Duration, error) {
		time.Sleep(50 * time.Millisecond)
		return fmt.Sprintf("value-%d", time.Now().UnixNano()), 5 * time.Second, nil
	}

	for i := 0; i < 5; i++ {
		v, _ := c.GetOrLoadCtx(context.Background(), "user:1001", loader)
		fmt.Println("read:", v)
		time.Sleep(1 * time.Second)
	}

	c.Delete("user:1001")
	c.Purge()
}
